package fyf.com.carddesign.Bll;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import fyf.com.carddesign.Dao.DaoSession;
import fyf.com.carddesign.Dao.User;
import fyf.com.carddesign.Dao.UserDao;
import fyf.com.carddesign.MyUtl.MyApplication;
import fyf.com.carddesign.MyUtl.MyUtl;
import fyf.com.carddesign.MyUtl.Tuple.Tuple;
import fyf.com.carddesign.MyUtl.Tuple.Tuple3;

/**
 * User业务逻辑层
 * Created by yxz on 2017/10/25.
 */

public class UserBll {
    private Context _context;
    private static UserBll _obj = null;
    private DaoSession _daoSession;

    public UserBll(Context _context) {
        this._context = _context;
        _daoSession = MyApplication.getDaoInstant();
    }

    public static UserBll getInstance(Context context) {
        if (null == _obj)
            _obj = new UserBll(context);
        return _obj;
    }

    /**
     * 查询Admin是否存在
     */
    public boolean selectAdmin() {
        UserDao userDao = _daoSession.getUserDao();
        List<User> list = userDao.queryBuilder()
                .where(UserDao.Properties.Type.eq(0))
                .build()
                .list();
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询语句判断用户名是否存在
     *
     * @param name 输入的用户名
     * @return true存在，false不存在
     */
    public boolean selectInfo(String name) {
        //当创建出User类时，使用插件会自动生成UserDao类
        UserDao userDao = _daoSession.getUserDao();
        List<User> list = userDao.queryBuilder()
                .where(UserDao.Properties.User_name.eq(name))//条件语句查询
                .build()
                .list();
        if (list.size() > 0) {
            Log.d("userData", list.get(0).getId() + "  name:" + list.get(0).getUser_name() + "  pwd:" +
                    list.get(0).getPassword() + "  type:" + list.get(0).getType());
            return true;
        }
        return false;
    }

    /**
     * 添加用户
     *
     * @param name     用户名
     * @param okPwd    密码
     * @param userType 用户类型
     */
    public String addUser(String name, String okPwd, Byte userType) {
        try {
            //当创建出User类时，使用插件会自动生成UserDao类
            UserDao userDao = _daoSession.getUserDao();
            User user = new User();
            user.setUser_name(name);
            user.setPassword(okPwd);
            user.setType(userType);
            userDao.insert(user);
            return "注册成功";
        } catch (Exception ex) {
            Log.d("insert", ex.getMessage());
            return ex.getMessage();
        }
    }

    /**
     * 校验登录信息
     *
     * @param name
     * @param pwd
     * @return 是否成功登录；提示信息；用户权限（管理员还是普通用户）
     */
    public Tuple3<Boolean, String, Byte> checkLoginMsg(String name, String pwd) {
        try {
            UserDao userDao = _daoSession.getUserDao();
            List<User> list = userDao.queryBuilder()
                    .where(UserDao.Properties.User_name.eq(name))
                    .build()
                    .list();
            if (list.size() <= 0) {
                return new Tuple3<>(false, "用户未注册", null);
            }
            if (!list.get(0).getPassword().equals(pwd)) {
                return new Tuple3<>(false, "登录密码错误", null);
            }
            return new Tuple3<>(true, "登录成功", list.get(0).getType());
        } catch (Exception ex) {
            return new Tuple3<>(false, ex.getMessage(), null);
        }
    }


    /**
     * 由上一层传递过来的
     *
     * @param tmp
     * @return
     */
    public List<String> getAllFilePath(String tmp) {
        List<String> list = new ArrayList<>();
        File f = new File(MyUtl.SD_HOME_DIR + tmp);
        File[] files = f.listFiles();
        List fileList = Arrays.asList(files);
        //对文件名进行一个排序
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.getName().compareTo(o2.getName());
            }
        });
//        String[] tmpStrAry = f.list();//这个至关重要，如果排序出错，一切都为0了
        for (int i = 0; i < files.length; i++) {
            list.add(files[i].getName());
            Log.d("yang_man", "----漫画名文件名--" + i + "  路径：" + list.get(i));
        }
        return list;
    }


}
